﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Translator.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains Translator class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IBupro.Classes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Resources;
    using System.Web;
    using System.Xml.Linq;

    /// <summary>
    /// Implementation of <see cref="ITranslator"/> that uses .NET resources system.
    /// </summary>
    public class Translator
    {
        private static Translator instance = null;

        private static string[] cultures = { "en-US", "cs-CZ" };
        private string locale = string.Empty;
        private Dictionary<string, string> data = null;
        private static Dictionary<string, Dictionary<string, string>> cache =
            new Dictionary<string, Dictionary<string, string>>();

        public static Translator Instance
        {
            get
            {
                return instance ?? (instance = new Translator());
            }
        }

        private Translator()
        {
        }

        public string Translate(string key)
        {
            if (this.data != null && this.data.ContainsKey(key))
            {
                return this.data[key];
            }
            else
            {
                return ":" + key + ":";
            }
        }

        public string this[string key]
        {
            get
            {
                return this.Translate(key);
            }
        }

        public string Locale
        {
            get
            {
                if (string.IsNullOrEmpty(this.locale))
                {
                    throw new Exception("Locale not set");
                }
                else
                {
                    return this.locale;
                }
            }

            set
            {
                if (Cultures.Contains(value))
                {
                    this.locale = value;
                    this.LoadXMLDocument();
                }
                else
                {
                    throw new Exception("Invalid locale");
                }
            }
        }

        private void LoadXMLDocument()
        {
            if (cache.ContainsKey(this.locale) == false) 
            {
                var doc = XDocument.Load(
                    HttpContext.Current.Server.MapPath(
                       "~/App_GlobalResources/" + locale + ".xml"));

                cache[this.locale] = (from item in doc.Descendants("item")
                                 where item.Attribute("key") != null
                                 select new
                                 {
                                     Key = item.Attribute("key").Value,
                                     Data = item.Value,
                                 }).ToDictionary(i => i.Key, i => i.Data);
            }

            this.data = cache[this.locale];
        }

        public static void ClearCache()
        {
            cache = new Dictionary<string, Dictionary<string, string>>();
        }

        public static string[] Cultures
        {
            get
            {
                return cultures;
            }
        }
    }
}